/*
 Copyright 2012 Ronald Pablos
 

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.bn.coders;

import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author Ronald
 */
public class Alphabets {
    static public final String NumericString_Alphabet = " 0123456789";
    
    static public String SortedSetAlphabetString(String str) {
        int[] caprimitive = SortedSetAlphabetUnicode(str);
        return new String(caprimitive,0,caprimitive.length);
    }
    
    static public int[] SortedSetAlphabetUnicode(String str) {
        Set<Integer> myset = new TreeSet<Integer>();
        for (int c: StringToUnicode(str))
            myset.add(c);
        Integer[] ia = myset.toArray(new Integer[0]);
        int[] caprimitive = new int[ia.length];
        for (int i = 0; i < ia.length; i++)
            caprimitive[i] = ia[i];
        return caprimitive;
    }
    
    static public int[] StringToUnicode(String str) {
        int len = str.length();
        int[] unicode = new int[str.codePointCount(0, len)];
        int cp;
        for (int i= 0,j=0; i<len; i += Character.charCount(cp)) {
            cp = str.codePointAt(i);
            unicode[j++] = cp;
        }
        return unicode;
    }
    static public String UnicodeToString(int[] str) {
        return new String(str,0,str.length);
    }
    
    static public int alphabetRange(int[] sortedsetalphabet) {
        if (sortedsetalphabet.length == 0)
            return 0;
        return sortedsetalphabet[sortedsetalphabet.length-1]-sortedsetalphabet[0]+1;
    }
    static public int alphabetRange(String sortedsetalphabet) {
        return alphabetRange(StringToUnicode(sortedsetalphabet));
    }
    public static int getMinimumNumerOfBitsForChar(String sortedsetalphabet) {
        return getMinimumNumerOfBitsForChar(StringToUnicode(sortedsetalphabet));
    }
    public static int getMinimumNumerOfBitsForChar(int[] sortedsetalphabet) {
        int range=sortedsetalphabet.length;
        return (int)Math.ceil(Math.log(range)/Math.log(2));
    }
    public static int getUpperBound(int[] sortedsetalphabet) {
        return (sortedsetalphabet.length > 0)?sortedsetalphabet[sortedsetalphabet.length-1]:0;
    }
    public static int getUpperBound(String sortedsetalphabet) {
        return getUpperBound(StringToUnicode(sortedsetalphabet));
    }
    
    static public int getNumerOfOctetsPerChar(int stringTag) {
        int noctets = 1; //default
        switch (stringTag) {
            case UniversalTag.BMPString: 
                noctets = 2;
                break;
            case UniversalTag.UniversalString:
                noctets = 4;
                break;
        }
        return noctets; 
    }
}